package com.example.shop.mapper;

import com.example.shop.entity.Cart;
import com.example.shop.model.resp.CartDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/9/2 21:42
 **/
@Mapper
public interface CartMapper {
    @Insert("""
INSERT INTO mi_cart (user_id, goods_id, sku_id, shop_id, nums, sold_out_status, 
create_time, update_time, operator_id, is_delete) 
VALUES (#{userId}, #{goodsId}, #{skuId}, #{shopId}, #{nums}, #{soldOutStatus}, now(), now(), null, 0);
""")
    void addCart(Cart cart);

    @Update("""
UPDATE mi_cart SET nums = #{nums}, update_time = NOW()
WHERE 
    user_id = #{userId}
    AND goods_id = #{goodsId}
    AND sku_id = #{skuId}
""")
    void updateCart(Cart cart);

    @Select("""
select * from mi_cart where user_id = #{userId};
""")
    List<Cart> queryUserCart(String userId);

    @Select("""
select * from mi_cart where user_id = #{userId} 
                        and goods_id = #{goodsId} 
                        and sku_id = #{skuId}
""")
    Cart queryUserCartByParm(@Param("userId") String userId,
                             @Param("goodsId") String goodsId,
                             @Param("skuId") String skuId);

    @Select("""
SELECT
    c.goods_id,
    c.nums AS quantity,
    s.sku_id AS skuKey,
    g.title,
    g.price,
    g.cover_image_id,
    g.shop_id as shopCode,
    s.cover_image_id AS image,
    s.attr AS spec
            FROM mi_cart c
            LEFT JOIN mi_goods g ON c.goods_id = g.goods_id
            LEFT JOIN mi_goods_sku s ON c.sku_id = s.sku_id AND c.goods_id = s.goods_id
            WHERE
    c.user_id = #{userId}
""")
    List<CartDetail> queryUserCartDetail(String userId);

    @Delete("""
delete from mi_cart where user_id = #{userId} 
                      and goods_id = #{goodsId} and sku_id = #{skuId}
""")
    void deleteCart(@Param("userId") String userId,
                    @Param("goodsId") String goodsId,
                    @Param("skuId") String skuId);
}
